<template>


  <div>
    <!--<el-button type="primary">我的按钮</el-button>-->
    <div style="margin: 10px 5px">
      <el-button type="primary" @click="add">新增</el-button>
      <el-button >其他</el-button>
    </div>
    <div style="margin: 10px 5px">
      <el-input v-model="seach" style="width: 240px" placeholder="Please input" />
      <el-button style="margin-left: 10px " type="primary" @click="listByNamePage">查询</el-button>
    </div>


    <el-table :data="tableData" stripe >
      <el-table-column sortable prop="id" label="编号"  />
      <el-table-column prop="name" label="家居名"  />
      <el-table-column prop="maker" label="厂商" />
      <el-table-column sortable prop="price" label="价格"  />
      <el-table-column prop="sales" label="销量"  />
      <el-table-column prop="stock" label="库存" />

      <el-table-column fixed="right" label="操作" width="120">
        <template #default="scope">
          <el-button link type="primary" size="small" @click="handleClick(scope.row)">更新</el-button>
          <el-popconfirm title="确认删除嘛?" confirm-button-type="danger" @confirm="handleDele(scope.row.id)">
            <template #reference>
              <el-button type="danger" size="small">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

  <!--  点击弹框（对话框）-->
    <el-dialog v-model="dialogFormVisible" title="提示" width="500">
      <el-form :model="form" :rules="rules" ref="form">
        <el-form-item label="家居名" prop="name">
          <el-input v-model="form.name" style="width: 50%"/>{{serverValidErrors.name}}
        </el-form-item>
        <el-form-item label="厂商" prop="maker">
          <el-input v-model="form.maker" style="width: 50%" />{{serverValidErrors.maker}}
        </el-form-item>
        <el-form-item label="价格" prop="price">
          <el-input v-model="form.price" style="width: 50%" />{{serverValidErrors.price}}
        </el-form-item>
        <el-form-item label="销量" prop="sales">
          <el-input v-model="form.sales" style="width: 50%" />{{serverValidErrors.sales}}
        </el-form-item>
        <el-form-item label="库存" prop="stock">
          <el-input v-model="form.stock" style="width: 50%" />{{serverValidErrors.stock}}
        </el-form-item>
      </el-form>

      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取消</el-button>
          <el-button type="primary" @click="save">
            确认
          </el-button>
        </div>
      </template>
    </el-dialog>

    <div style="margin: 10px 0">
      <el-pagination
          @size-change="handlePageSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[2,5,10]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>

  </div>

</template>

<script>
// @ is an alias to /src
// import HelloWorld from '@/components/HelloWorld.vue'
import request from "@/utils/request";
export default {
  name: 'HomeView',
  components: {
  },
  data() {
    return {
      seach:'',
      dialogFormVisible:false,
      form:{},
      total:'',
      currentPage:1,
      pageSize:5,
      tableData: [
        // {
        //   date: '2016-05-03',
        //   name: 'Tom',
        //   address: 'No. 189, Grove St, Los Angeles',
        // },
        // {
        //   date: '2016-05-02',
        //   name: 'Tom',
        //   address: 'No. 189, Grove St, Los Angeles',
        // },
        // {
        //   date: '2016-05-04',
        //   name: 'Tom',
        //   address: 'No. 189, Grove St, Los Angeles',
        // },
        // {
        //   date: '2016-05-01',
        //   name: 'Tom',
        //   address: 'No. 189, Grove St, Los Angeles',
        // }
      ],
      rules:{
        name:[
          {required:true,message:"请输入家居名",trigger:"blur"}
        ],
        maker:[
          {required:true,message:"请输入制造商",trigger:"blur"}
        ],
        price:[
          {required:true,message:"请输入价格",trigger:"blur"},
          {pattern: /^([1-9]\d*|0)(\.\d+)?$/,message: "请输入数字",trigger:"blur"}
        ],
        sales:[
          {required:true,message:"请输入销量",trigger:"blur"},
          {pattern: /^([1-9]\d*|0)$/,message: "请输入数字",trigger:"blur"}

        ],
        stock:[
          {required:true,message:"请输入库存",trigger:"blur"},
          {pattern: /^([1-9]\d*|0)$/,message: "请输入数字",trigger:"blur"}

        ]
      },
      serverValidErrors:{}
    }
  },
  created() {
    this.listByNamePage();
  },
  methods:{
    add(){
      this.dialogFormVisible = true
      this.form = {}
      this.$refs.form.resetFields()
      this.serverValidErrors={}
    },
    //循环问题初始
    save(){
      if (this.form.id) {
        request.post("/api/updataFurn",this.form).then(res => {
          if (res.code === 200){
            this.$message(
                {
              type:"success",
              message:"更新成功"
            })
          }else{
            this.$message(
                {
                  type:"error",
                  message:"更新失败"
                }
          )
          }
          this.dialogFormVisible = false
             this.listByNamePage()
        }
        )
      } else {
        this.$refs.form.validate((valid) => {

          if (valid){
            request.post("/api/save", this.form).then(res => {
              // console.log("res-", res)
              if (res.code === 200){
                this.dialogFormVisible = false
                this.listByNamePage()
              }else if (res.code === 400){
                this.serverValidErrors.name = res.extend.errors.name
                this.serverValidErrors.maker = res.extend.errors.maker
                this.serverValidErrors.price = res.extend.errors.price
                this.serverValidErrors.sales = res.extend.errors.sales
                this.serverValidErrors.stock = res.extend.errors.stock
              }
            })

          } else {
            this.$message({

              type:"error",
              message:"验证失败，无法提交！"
            })
            return false
          }
        })
    }},
    //显示所有家居
    list(){
      // request.get("/api/furns").then(res => {
      //
      //   console.log("res-",res)
      //   //res在request中进行了处理
      //   this.tableData = res.extend.furnList
      // })

      request.get("/api/pageFurnList",{
        params:{
          pageNum:this.currentPage,
          pageSize:this.pageSize
        }
      }).then(res => {
        this.tableData = res.extend.pageInfo.list
        this.total = res.extend.pageInfo.total
      })
    },
    handleClick(row){
      this.form = JSON.parse(JSON.stringify(row));
      this.dialogFormVisible = true
    },
    handleDele(id){
      request.post("/api/deleteFurn/"+id).then(res => {
        if (res.code === 200) {
          this.$message(
              {
                type: "success",
                message: res.msg
              })
        } else {
          this.$message(
              {
                type: "error",
                message: res.msg
              }
          )
        }

        this.listByNamePage()
      })
    },
    handleCurrentChange(pageNum){
      // console.log("pageNum",pageNum)
      this.currentPage = pageNum
      this.listByNamePage()
    },
    handlePageSizeChange(pageSize){
      // console.log("pageNum-",pageSize)
      this.pageSize = pageSize
      this.listByNamePage()

    },
    listByNamePage(){
      // request.get("/api/furns").then(res => {
      //
      //   console.log("res-",res)
      //   //res在request中进行了处理
      //   this.tableData = res.extend.furnList
      // })

      request.get("/api/pageFurnListByName",{
        params:{
          pageNum:this.currentPage,
          pageSize:this.pageSize,
          name:this.seach
        }
      }).then(res => {
        this.tableData = res.extend.pageInfo.list
        this.total = res.extend.pageInfo.total
      })
    }

  }
}
</script>
